Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
Cannot send data from aspx file to code behind using $.ajax({ type: "POST", using VS2017 C#

I'm using web-forms to collect data from a form and then send the data to the code-behind to send to an API. Using a button I'm calling a JavaScript method which collates the data and then sends to my aspx.cs file to send to the API. The Html code for the button is

<button class="search btn" ID="btnSearch" onclick="searchApi(); return false;"><i class="fas fa-search"></i>Search</button>

This runs the searchAPI() function which works and creates a concatenated string called SearchData. The Javascript code looks like this

var searchString = JsonData;
var trimsearchString = searchString.replace(/,/g, '');

$.ajax({
   type: "POST",
   url: 'Default.aspx/GetApi',
      data: searchString,
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function (data) {
          alert('success');
                },
                error: function (errordata) {
                    console.log(errordata);
                    alert(errordata);
                }
    });

The method GetAPI in my Default.aspx.cs file is never called. The method code is

[System.Web.Services.WebMethod]
public static void GetApi(string searchData)    
{...

The success: function (data) returns success but the code behind method is never called, can someone please tell me what I am missing.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

fix the ajax data, it seems that method with this parameter can' t be found

$.ajax({
   type: "POST",
   url: 'Default.aspx/GetApi',

      data: { searchData: trimsearchString},
      //or if it is still problem, you can even try
       data: JSON.stringify( { searchData: trimsearchString}),

      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function (data) {
          alert(data);
                },
                error: function (errordata) {
                    console.log(errordata);
                    alert(errordata);
                }
    });

and your webmethod should return something

[WebMethod]
public static string GetApi(string searchData)    
{
   return searchData
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!